<extend name="Common/back" xmlns="http://www.w3.org/1999/html"/>
<block name="content">
        <div id="content">
            <div class="page-header">
                <div class="container-fluid">
                    <div class="pull-right">
                        <button type="submit" form="form-set" data-toggle="tooltip" title="保存" class="btn btn-primary">
                            <i class="fa fa-save"></i>
                        </button>
                        <a href="{:U('list')}" data-toggle="tooltip" title="取消" class="btn btn-default">
                            <i class="fa fa-reply"></i>
                        </a>
                    </div>
                    <h1>商品</h1>
                    <ul class="breadcrumb">
                        <li>
                            <a href="{:U('Manage/index')}">首页</a>
                        </li>
                        <li>
                            <a href="javascript:;">商品</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="container-fluid">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">
                            <i class="fa fa-pencil"></i>
                            设置商品
                        </h3>
                    </div>
                    <div class="panel-body">
                        <form action="{:U('set')}" method="post" enctype="multipart/form-data" id="form-set"
                        class="form-horizontal">
                            <if condition="isset($data['goods_id'])">
                                <input type="hidden" name="goods_id" value="{$data['goods_id']}" id="input-goods_id">
                            </if>
                            <ul class="nav nav-tabs">
                                <li class="active">
                                    <a href="#tab-general" data-toggle="tab">基本信息</a>
                                </li>
                                <li>
                                    <a href="#tab-description" data-toggle="tab">商品描述</a>
                                </li>
                                <li>
                                    <a href="#tab-seo" data-toggle="tab">SEO项目</a>
                                </li>
                                <li>
                                    <a href="#tab-links" data-toggle="tab">关联项目</a>
                                </li>
                                <li>
                                    <a href="#tab-attribute" data-toggle="tab">属性</a>
                                </li>
                                <li>
                                    <a href="#tab-discount" data-toggle="tab">促销活动</a>
                                </li>
                                <li>
                                    <a href="#tab-image" data-toggle="tab">图片设置</a>
                                </li>
                            </ul>
              <div class="tab-content">
                    <div class="tab-pane active" id="tab-general">

                                    <div class="form-group">
                    <label class="col-sm-2 control-label" for="input-name">商品</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="商品"
                     name="name" id="input-name" value="{$data['name']}"/>
                     <if condition="isset($message['name'])">
                     <label for="input-name" class="text-danger">{$message['name']}</label>
                  </if>
                 </div>
                </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-image">原始图像</label>
                            <div class="col-sm-6">
                                <input type="hidden" name="image" id="input-image-value">
                                <input type="hidden" name="image_thumb" id="input-image_thumb-value">
                                <span class="btn btn-success fileinput-button">
                                            <i class="glyphicon glyphicon-plus"></i>
                                            <span>添加文件</span>
                                            <input id="input-image" name="image" type="file"
                                         data-uploadurl="{:U('upload')}">
                          </span>
                                <if condition="isset($message['image'])">
                                    <label for="input-image" class="text-danger">{$message['image']}</label>
                                </if>
                            </div>
                            <div class="class-sm-4">
                                <img src="__PUBLIC__/Thumb/{$data['image_thumb']}" alt="" id="image-show"
                                     class="img-thumbnail" style="max-height: 50px; <if condition=" $data['image']
                                eq ''">display:none;</if>" />
                            </div>
                        </div>
                        <div class="form-group">
                    <label class="col-sm-2 control-label" for="input-quantity">库存</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="库存"
                     name="quantity" id="input-quantity" value="{$data['quantity']|default=1}"/>
                     <if condition="isset($message['quantity'])">
                     <label for="input-quantity" class="text-danger">{$message['quantity']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-sku_id">库存单位</label>
                      <div class="col-sm-10">
                     <select class="form-control" name="sku_id"  id="input-sku_id"
                             data-checkurl="{:U('check', ['field'=>'sku_id'])}>
                         <volist name="sku_list" id="sku">
                             <option value="{$sku['sku_id']}"
                                 <if condition="$data['sku_id'] eq $sku['sku_id']">
                         selected
                                 </if> >
                                 {$sku['title']}
                             </option>
                         </volist>
                     </select>
                     <if condition="isset($message['sku_id'])">
                     <label for="input-sku_id" class="text-danger">{$message['sku_id']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-upc">商品编码</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="商品编码"
                     name="upc" id="input-upc" value="{$data['upc']}"/>
                     <if condition="isset($message['upc'])">
                     <label for="input-upc" class="text-danger">{$message['upc']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-price">价格</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="价格"
                     name="price" id="input-price" value="{$data['price']}"/>
                     <if condition="isset($message['price'])">
                     <label for="input-price" class="text-danger">{$message['price']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-market_price">市场价</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="市场价"
                     name="market_price" id="input-market_price" value="{$data['market_price']}"/>
                     <if condition="isset($message['market_price'])">
                     <label for="input-market_price" class="text-danger">{$message['market_price']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-tax_id">税类型</label>
                      <div class="col-sm-10">
                     <select class="form-control"
                     name="tax_id" id="input-tax_id" >
                          <volist name="tax_list" id="tax">
                              <option value="{$tax['tax_id']}"> <if condition="$data['tax_id'] eq
                             $tax['tax_id']">
                                  selected
                              </if>
                                  {$tax['title']}
                              </option>
                          </volist>
                      </select>
                     <if condition="isset($message['tax_id'])">
                     <label for="input-tax_id" class="text-danger">{$message['tax_id']}</label>
                  </if>
                 </div>
                </div>
                    <div class="form-group">
                    <label class="col-sm-2 control-label" for="input-minimum">起售数量</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="起售数量"
                     name="minimum" id="input-minimum" value="{$data['minimum']}"/>
                     <if condition="isset($message['minimum'])">
                     <label for="input-minimum" class="text-danger">{$message['minimum']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-subtract">是否减少库存</label>
                      <div class="col-sm-10">
                     <input type="checkbox" class="form-control"
                     name="subtract" id="input-subtract" value="1" <if condition="$data['subtract'] eq
                       1 OR !isset($data['subtract'])">checked</if> />
                     <if condition="isset($message['subtract'])">
                     <label for="input-subtract" class="text-danger">{$message['subtract']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-stock_status_id">库存状态</label>
                      <div class="col-sm-10">
                     <select  class="form-control"
                     name="stock_status_id" id="input-stock_status_id">
                         <volist name="stock_status_list" id="stock_status">
                             <option value="{$stock_status['stock_status_id']}" <if
                                 condition="$data['stock_status_id'] eq $stock_status['stock_status_id']">selected</if> >
                             {$stock_status['title']}

                             </option>
                         </volist>
                     </select>
                     <if condition="isset($message['stock_status_id'])">
                     <label for="input-stock_status_id" class="text-danger">{$message['stock_status_id']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-shipping">是否配送</label>
                      <div class="col-sm-10">
                     <input type="checkbox" class="form-control"
                     name="shipping" id="input-shipping" value="1" <if condition="$data['shipping'] eq
                     1 OR !isset($data['shipping'])" >checked</if> />
                     <if condition="isset($message['shipping'])">
                     <label for="input-shipping" class="text-danger">{$message['shipping']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-date_available">起售日期</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="起售日期"
                     name="date_available" id="input-date_available" value="{$data['date_available']}"/>
                     <if condition="isset($message['date_available'])">
                     <label for="input-date_available" class="text-danger">{$message['date_available']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-length">长</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="长"
                     name="length" id="input-length" value="{$data['length']}"/>
                     <if condition="isset($message['length'])">
                     <label for="input-length" class="text-danger">{$message['length']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-width">宽</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="宽"
                     name="width" id="input-width" value="{$data['width']}"/>
                     <if condition="isset($message['width'])">
                     <label for="input-width" class="text-danger">{$message['width']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-height">高</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="高"
                     name="height" id="input-height" value="{$data['height']}"/>
                     <if condition="isset($message['height'])">
                     <label for="input-height" class="text-danger">{$message['height']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-length_unit_id">长度单位</label>
                      <div class="col-sm-10">
                     <select  class="form-control"
                     name="length_unit_id" id="input-length_unit_id" >
                         <volist name="length_unit_list" id="length_unit">
                             <option value="{$length_unit['length_unit_id']}" <if condition="$data['length_unit_id']
          eq $length_unit['length_unit_id']">selected</if> >{$length_unit['title']}

                             </option>
                         </volist>
                     </select>
                     <if condition="isset($message['length_unit_id'])">
                     <label for="input-length_unit_id" class="text-danger">{$message['length_unit_id']}</label>
                  </if>
                 </div>
                </div>
                    <div class="form-group">
                    <label class="col-sm-2 control-label" for="input-weight">重量</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="重量"
                     name="weight" id="input-weight" value="{$data['weight']}"/>
                     <if condition="isset($message['weight'])">
                     <label for="input-weight" class="text-danger">{$message['weight']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-weight_unit_id">重量单位</label>
                      <div class="col-sm-10">
                     <select class="form-control"
                     name="weight_unit_id" id="input-weight_unit_id" >
                         <volist name="weight_unit_list" id="weight_unit">
                             <option value="{$weight_unit['weight_unit_id']}" <if condition="$data['weight_unit_id']
          eq $weight_unit['weight_unit_id']">selected</if> >{$weight_unit['title']}

                             </option>
                         </volist>
                     </select>
                     <if condition="isset($message['weight_unit_id'])">
                     <label for="input-weight_unit_id" class="text-danger">{$message['weight_unit_id']}</label>
                  </if>
                 </div>
                </div><div class="form-group">
                    <label class="col-sm-2 control-label" for="input-status">状态</label>
                      <div class="col-sm-10">
                     <input type="checkbox" class="form-control"
                          name="status" id="input-status" value="1" <if condition="$data['status'] eq 1
                          OR !isset($data['status'])">checked</if> />
                     <if condition="isset($message['status'])">
                     <label for="input-status" class="text-danger">{$message['status']}</label>
                  </if>
                 </div>
                </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label" for="input-type_id">类型</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" placeholder="类型"
                                       name="type_id" id="input-type_id" value="{$data['type_id']}"/>
                                <if condition="isset($message['type_id'])">
                                    <label for="input-type_id" class="text-danger">{$message['type_id']}</label>
                                </if>
                            </div>
                        </div>




                  <div class="form-group">
                    <label class="col-sm-2 control-label" for="input-sort_number">排序</label>
                      <div class="col-sm-10">
                     <input type="text" class="form-control" placeholder="排序"
                     name="sort_number" id="input-sort_number" value="{$data['sort_number']}"/>
                     <if condition="isset($message['sort_number'])">
                     <label for="input-sort_number" class="text-danger">{$message['sort_number']}</label>
                  </if>
                 </div>
                </div>
         </div>
                  <div class="tab-pane" id="tab-description">
                  <div class="form-group">
                      <label class="col-sm-2 control-label" for="input-description">描述</label>
                      <div class="col-sm-10">
                              <textarea class="form-control" placeholder="描述" name="description"
                                        id="input-description"
                                        data-uploadurl="{:U('upload',['type'=>'description'])}">{$data['description']}</textarea>
                          <if condition="isset($message['description'])">
                              <label for="input-description" class="text-danger">{$message['description']}</label>
                          </if>
                      </div>
                  </div>
              </div>
                  <div class="tab-pane" id="tab-seo">
                      <div class="form-group">
                          <label class="col-sm-2 control-label" for="input-meta_title">META标题</label>
                          <div class="col-sm-10">
                              <input type="text" class="form-control" placeholder="META标题"
                                     name="meta_title" id="input-meta_title" value="{$data['meta_title']}"/>
                              <if condition="isset($message['meta_title'])">
                                  <label for="input-meta_title" class="text-danger">{$message['meta_title']}</label>
                              </if>
                          </div>
                      </div><div class="form-group">
                      <label class="col-sm-2 control-label" for="input-meta_keywords">META关键字</label>
                      <div class="col-sm-10">
                          <input type="text" class="form-control" placeholder="META关键字"
                                 name="meta_keywords" id="input-meta_keywords" value="{$data['meta_keywords']}"/>
                          <if condition="isset($message['meta_keywords'])">
                              <label for="input-meta_keywords" class="text-danger">{$message['meta_keywords']}</label>
                          </if>
                      </div>
                  </div><div class="form-group">
                      <label class="col-sm-2 control-label" for="input-meta_description">META描述</label>
                      <div class="col-sm-10">
                          <textarea  class="form-control" placeholder="META描述"
                                     name="meta_description" id="input-meta_description"> {$data['meta_description']}</textarea>
                          <if condition="isset($message['meta_description'])">
                              <label for="input-meta_description" class="text-danger">{$message['meta_description']}</label>
                          </if>
                      </div>
                  </div>
                  </div>
                   <div class="tab-pane" id="tab-links">
                       <div class="form-group">
                           <label class="col-sm-2 control-label" for="input-brand_id">品牌</label>
                           <div class="col-sm-10">
                               <select class="form-control"  name="brand_id" id="input-brand_id">
                                   <volist name="brand_list" id="brand">
                                       <option value="{$brand['brand_id']}" <if condition="$data['brand_id']
                               eq $brand['brand_id']">selected</if> >
                                           {$brand['title']}
                                       </option>
                                   </volist>
                               </select>
                               <if condition="isset($message['brand_id'])">
                                   <label for="input-brand_id" class="text-danger">{$message['brand_id']}</label>
                               </if>
                           </div>
                       </div>
                       <div class="form-group">
                           <label class="col-sm-2 control-label" for="input-category">分类</label>
                           <div class="col-sm-10">
                               <select class="form-control" name="category_id[]" id="input-category"  multiple size="10">
                                   <volist name="category_list" id="category">
                                       <option value="{$category['category_id']}"
                                       <if condition="in_array($category['category_id'],$category_linked)">selected</if> >
                                       {:str_repeat('&nbsp;',  $category['deep']*2)}{$category['category_title']}</option>
                                   </volist>
                               </select>
                               <if condition="isset($message['category_id'])">
                                   <label for="input-category" class="text-danger">{$message['category_id']}</label>
                               </if>
                           </div>
                       </div>
                   </div>

                  <div class="tab-pane" id="tab-image">
                      <div class="table-responsive">
                          <table id="table-galleries" class="table table-striped table-bordered table-hover"
                                 data-removeurl="{:U('remove')}">
                              <thead>
                              <tr>
                                  <td class="col-sm-4 text-left">图片</td>
                                  <td  class="col-sm-4 text-left">描述</td >
                                  <td  class="col-sm-4 text-right">排序</td >
                                  <td></td>
                              </tr>
                              </thead>
                              <tbody>
                              <volist name="gallery_list" id="gallery">
                                  <tr id="image-row-{$key}">
                                      <td class="text-left">
                                          <input  type="hidden" name="galleries[{$key}][gallery_id]" value="{$gallery['gallery_id']}">
                                          <img src="__PUBLIC__/Thumb/{$gallery['image_small']}" alt="NO-IMG" title="">
                                      </td>
                                      <td class="text-right">
                                          <input name="galleries[{$key}][intro]" value="{$gallery['intro']}"
                                                 placeholder="描述" class="form-control" type="text">
                                      </td>
                                      <td class="text-right">
                                          <input name="galleries[{$key}][sort_number]" value="{$gallery['sort_number']}"
                                                 placeholder="排序" class="form-control valid" aria-invalid="false" type="text">
                                      </td>
                                      <td class="text-left">
                                          <button type="button" data-toggle="tooltip" title=""
                                                  class="btn btn-danger remove-button" data-original-title="移除"
                                          data-gallery_id="{$gallery['gallery_id']}"><i class="fa fa-minus-circle"></i></button>
                                      </td>
                                  </tr>
                              </volist>
                              </tbody>
                              <tfoot>
                              <tr>
                                  <td  colspan="4" class="text-right">

                                                        <span class="btn btn-success fileinput-button">
                                                            <i class="glyphicon glyphicon-plus"></i>
                                                            <span >选择图片</span>
                                 <input id="input-galleries" name="galleries" type="file"
                                        multiple data-uploadurl="{:U('upload', ['type'=>'gallery'])}">
                                                        </span>
                                  </td>
                              </tr>
                              </tfoot>
                          </table>
                      </div>
                            </div>
              </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </block>
    <block name="appendCss">
        <link href="__PUBLIC__/Back/datetimepicker/bootstrap-datetimepicker.min.css"
              type="text/css" rel="stylesheet" media="screen">
        <link href="__PUBLIC__/Back/summernote/summernote.css" type="text/css"
              rel="stylesheet" media="screen">
        <link href="__PUBLIC__/Back/jqueryFileUpload/css/jquery.fileupload.css"
              type="text/css" rel="stylesheet" media="screen">
        <link href="__PUBLIC__/Back/jqueryFileUpload/css/jquery.fileupload-ui.css"
              type="text/css" rel="stylesheet" media="screen">
    </block>
    <block name="appendJs">
        <script src="__PUBLIC__/Back/validate/jquery.validate.min.js"></script>
        <script src="__PUBLIC__/Back/validate/additional-methods.min.js"></script>
        <script src="__PUBLIC__/Back/validate/localization/messages_zh.min.js"></script>
        <script src="__PUBLIC__/Back/datetimepicker/moment.js"></script>
        <script src="__PUBLIC__/Back/datetimepicker/bootstrap-datetimepicker.min.js"></script>
        <script src="__PUBLIC__/Back/summernote/summernote.js"></script>
        <script src="__PUBLIC__/Back/jqueryFileUpload/js/vendor/jquery.ui.widget.js"></script>
        <script src="__PUBLIC__/Back/jqueryFileUpload/js/jquery.fileupload.js"></script>
        <script>
            $(function(){
                $('#input-date_available').datetimepicker({
                    pickDate: true,
                    pickTime: false,
                    format: 'YYYY-MM-DD',
                });
            });
        </script>
        <script>
            $(function () {
                var editor=$('#input-description').summernote({
                    height:400,
                    callbacks:{
                        onImageUpload:function (fileList) {

                            var data=new FormData();

                            data.append('image',fileList[0]);
                            $.ajax({
                                type:'post',
                                url: $('#input-description').data('uploadurl'),
                                data: data,
                                contentType:false,
                                processData:false,
                                success: function (response) {
                                    if(response.error===0){
                                        //成功了
                                        editor.summernote('insertImage',$('body').data('root')+'Upload/'+response.image);
                                    }

                                }
                            });
                        }
                    }
                });
            })
        </script>

        <script>
            $(function(){
                var validator= $('#form-set').validate({
                    rules:{
                        sku_id:{
                            remote:$('#input-sku_id').data('checkurl')
                        }
                    },
                    messages:{
                        sku_id:{
                            remote:'选择合理的库存单位',
                        }
                    },
                    errorClass:'text-danger',
                });
                $('#input-sku_id').change(function(evt) {
                    validator.element($(this));
                });
            });
        </script>
        <script>
            $(function () {
                $('#input-image').fileupload({
                    // 负责完成动作
                    url: $('#input-image').data('uploadurl'),
                    dataType: 'json',
                    // 上传成功后, 回调的函数,
                    done: function (evt, response) {
                        var data = response.result;
                        if (0 === data.error) {
                            // 将图像反显
                            $('#image-show').attr('src', $('body').data('public') + 'Thumb/' + data.image_thumb).show();
                            $('#input-image-value').val(data.image);
                            $('#input-image_thumb-value').val(data.image_thumb);
                        }
                    }
                });
            })
        </script>
        <script>
            $(function(){
                //移出绑定事件
                $('#table-galleries').delegate('button.remove-button','click',function(evt) {
                    var currButton=$(this);
                    //ajax删除服务器上的记录数和图像
                    var url=$('#table-galleries').data('removeurl');

                    var data={
                        gallery_id:currButton.data('gallery_id'),
                        image_small:currButton.data('image_small')
                    };
                    $.post(url,data,function(response){
                        //删除所在的行
                        currButton.parents('tr').remove();
                    },'json');
                    evt.preventDefault();
                })
            })
        </script>
        <script>

            $(function () {
                // 初始化一个变量
                var galleryIndex = $('#table-galleries>tbody>tr').size();
                $('#input-galleries').fileupload({
                    url: $('#input-galleries').data('uploadurl'),
                    datatype: 'json',
                    done: function(evt, response)
                    {
                        var data = response.result;
                        if (data.error === 0) {
                            // 没出错完成反显
                            var html = '<tr id="image-row-' + galleryIndex +
                                '">' +
                                '<td class="text-left">' +
                                '<input type="hidden" name="galleries[' + galleryIndex +
                                '][image]" value="' + data.image +
                                '">' +
                                '<input type="hidden" name="galleries[' + galleryIndex +
                                '][image_big]" value="' + data.image_big +
                                '">' +
                                '<input type="hidden" name="galleries[' + galleryIndex +
                                '][image_medium]" value="' + data.image_medium +
                                '">' +
                                '<input type="hidden" name="galleries[' + galleryIndex +
                                '][image_small]" value="' + data.image_small +
                                '">' +
                                '<img src="' + $('body').data('public') + 'Thumb/' + data.image_small +
                                '" alt="NO-IMG" title="">' +
                                '</td>' +
                                '<td class="text-right">' +
                                '<input name="galleries[' + galleryIndex +
                                '][intro]" value="" placeholder="描述" class="form-control" type="text">' +
                                '</td>' +
                                '<td class="text-right">' +
                                '<input name="galleries[' + galleryIndex +
                                '][sort_number]" value="0" placeholder="排序" class="form-control" type="text">' +
                                '</td>' +
                                '<td class="text-left">' +
                                '<button type="button" data-toggle="tooltip" title="移出" class="btn btn-danger remove-button"'+' data.image_small="'+data.image_small+'"'+'>'+
                                '<i class="fa fa-minus-circle"></i></button>' +
                                '</td>' +
                                '</tr>';
                            $('#table-galleries>tbody').append(html);

                            // 索引递增
                            ++ galleryIndex;
                        }
                    }
                });
            })
        </script>


    </block>